package handler

import (
	"context"
	"gitee.com/gopher2011/go-zero-gva/internal/logic/upload"
	"gitee.com/gopher2011/go-zero-gva/internal/svc"
	"gitee.com/gopher2011/go-zero-gva/internal/types"
	"github.com/gogf/gf/util/gvalid"
	"net/http"

	"github.com/tal-tech/go-zero/rest/httpx"
)

func GetFileList(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.Request
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := upload.NewUpload(r.Context(), ctx)
		resp, err := l.GetFileList(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}

func DeleteFile(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.Request
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := upload.NewUpload(r.Context(), ctx)
		resp, err := l.DeleteFile(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}

func BreakpointContinue(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.Request
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := upload.NewUpload(r.Context(), ctx)
		resp, err := l.BreakpointContinue(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}

func FindFile(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.Request
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := upload.NewUpload(r.Context(), ctx)
		resp, err := l.FindFile(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}

func BreakpointContinueFinish(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.Request
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := upload.NewUpload(r.Context(), ctx)
		resp, err := l.BreakpointContinueFinish(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}

func RemoveChunk(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.Request
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := upload.NewUpload(r.Context(), ctx)
		resp, err := l.RemoveChunk(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}

func UploadFile(ctx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.Request
		if err := httpx.Parse(r, &req); err != nil {
			httpx.Error(w, err)
			return
		}
		if err := gvalid.CheckStruct(context.TODO(), &req, nil); err != nil {
			httpx.Error(w, err)
			return
		}
		l := upload.NewUpload(r.Context(), ctx)
		resp, err := l.UploadFile(req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}

